define("mainform", ["mustache", "bootstrap"], function(mustache) {
	"use strict";

	function Init() {
		var uid = vw_static.getCookie(window.location.host);
		var uName = vw_static.getCookie(uid).split('|')[0];
		var $container = $('.vw-page-container');

		vw_common.customAjax({
			url: VW_INTERFACE.WEBSERVICE.LoginManage("GetFunction"),
			type: "POST",
			dataType: "json",
			data: {
				"userID": uid,
			}
		}, function(e) {
			if(!e.function.length) {
				return;
			}

			var curItems = [];
			$.each(e.function, function(k, val) {
				if(val.isEnabled) {
					val.url += "?fid=" + val.id + "&fn=" + vw_static.compileStr(val.title);
				} else {
					val.url = "#";
					val.color = "#95a5a6";
				}
				if(k % 4 == 0) {
					curItems.push({
						"items": []
					});
				}
				curItems[curItems.length - 1].items.push(val);
			});

			var html = [
				'{{#rows}}<div class="row">',
				'	{{#items}}<div class="col-md-3">' +
				'		<a class="fitem tile {{#selected}}selected{{/selected}}" style="background-image:none;background-color:{{color}};" href="{{url}}">',
				'			{{#corner}}<div class="corner"></div>{{/corner}}',
				' 			{{#check}}<div class="check"></div>{{/check}}',
				'			<div class="tile-body">',
				'				<i class="{{icon}}"></i>',
				'			</div>',
				'			<div class="tile-object">',
				'				<div class="name" name="{{name}}">{{title}}</div>',
				'			</div>',
				'		</a>',
				'	</div>{{/items}}',
				'</div>{{/rows}}'
			].join('');

			$container.append(mustache.render(html, {
				rows: curItems
			}));

			resize();
			$(window).resize(resize);

			$(".fitem").on("click", function(e) {
				var fn = $(this).find('.tile-object .name').text();
				vw_common.ConLog(uName, "enterFn|" + fn);
			});
		});
	}

	function resize() {
		var headerHeight = $('#vw-navbar').outerHeight(true);
		var contentHeight = $(window).height() - headerHeight;
		var iconHeight = $('.tile .tile-body>i').css('font-size').replace('px', '');
		var tileHeight = (contentHeight - 10) / 4 - 20;

		$('.tile .tile-body').css({
			"padding": (tileHeight - iconHeight) / 2 + "px 40px",
		});

		$('.tile .tile-object>.name').css({
			"margin-bottom": (tileHeight - 22) / 2 + "px",
			"font-size": 22 + "px",
		});

		$(".vw-scroll-container").css({
			"padding-top": (contentHeight - $('.vw-page-container').height()) / 2 + "px",
			"overflow-y": "auto",
			"max-height": contentHeight + "px"
		});
	}

	return {
		init: Init
	}
})